<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索和添加 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入订单编号" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe @expand-change="getOrderDetail">
        <el-table-column label="明细" type="expand">
          <template slot-scope="scope">
            <!-- 订单下的商品列表 -->
            <el-table :data="scope.row.detail" border stripe>
              <el-table-column label="封面" width="100px" header-align="center" align="center">
                <template slot-scope="sc">
                  <!-- 实现图片点击放大效果 -->
                  <el-popover placement="top-start" trigger="hover">
                    <img :src="sc.row.url" style="width: 150px; height: 150px" />
                    <img :src="sc.row.url" slot="reference" style="width: 50px; height: 50px" />
                  </el-popover>
                </template>
              </el-table-column>
              <el-table-column label="商品名称" prop="goods_name"></el-table-column>
              <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
              <el-table-column label="商品数量" prop="goods_number" width="100px"></el-table-column>
              <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
              <el-table-column label="总价(元)" prop="goods_total_price" width="100px"></el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格(元)" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" type="success" v-if="scope.row.pay_status === 1">已付款</el-tag>
            <el-tag effect="dark" size="mini" type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="修改收货地址" placement="top" :enterable="false">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="showAddressDialog(scope.row.order_id)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="查看物流信息" placement="top" :enterable="false">
              <el-button type="warning" size="mini" icon="el-icon-location" @click="showProgressDialog()"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getOrderList()
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagesize: 10,
        pagenum: 1,
      },
      orderList: [],
      total: 0,
      addressVisible: false,
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      console.log('getOrderList res', res)
      
      // 不能this.orderList = JSON.parse(res.data)，this.orderList.forEach否则显示图片有问题
      const xx = JSON.parse(res.data)
      xx.forEach(item => {
          item.create_time = this.$utils.formatToTime(item.create_time)
          item.detail = []
      })
      this.orderList = xx
      this.total = res.total
    },
    async getOrderDetail(row) {
        // console.log('getOrderDetail row', row)
        const { data: res } = await this.$http.get(`orders/${row.order_id}`)
        console.log('getOrderDetail res', res)
        // goods只返回了goods_id,格式[10022,10023]
        row.detail = res.goods
        const goods_len = row.detail.length
        row.detail.forEach(async item => {
            const { data: res } = await this.$http.get(`goods/${item}`)
            console.log('getOrderDetail goods res', res)
            row.detail.push(res)
            this.$set(res,'url','http://192.168.43.154:8888/' + res.pics[0].pic)
        })
        // 删除goods_id,[10022,10023]
        row.detail.splice(0,goods_len)
        console.log('orderList', this.orderList)
    },
    showAddressDialog() {},
    showProgressDialog() {},
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize
      this.getOrderList()
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum
      this.getOrderList()
    },
  },
}
</script>
<style lang="less" scoped>
</style>